package cn.tju.yanbo.commons;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.tju.yanbo.R;
import cn.tju.yanbo.commons.anim.AnimationController;
import cn.tju.yanbo.commons.anim.AppAnimationUtils;
import cn.tju.yanbo.utils.AppConfig;
import cn.tju.yanbo.utils.AppUtils;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.widget.ImageView;


public class CalendarGestureListener implements OnGestureListener, OnTouchListener {

    protected AnimationController controller = null;
    protected BaseActivity activity = null;
    private GestureDetector detector = null;
    
    public CalendarGestureListener(BaseActivity activity) {
        detector = new GestureDetector(this);
        this.activity = activity;
        this.controller =  new AnimationController(activity);
    }
    
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {
        Point p1 = new Point((int)e1.getX(), (int)e1.getY());
        if(p1.y>AppConfig.DATE_COMPASS_CENTROID.y && AppUtils.isPointInCompass(p1, activity.center(), activity.compassRadius())) {
            return revolveDateView(e1, e2, velocityX, velocityY);
        } else {
            return switchActivity(e1, e2, velocityX, velocityY);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent ev) {
        return detector.onTouchEvent(ev);
    }

    protected boolean switchActivity(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {
        int len4judge = AppUtils.SCREEN_METRICS.widthPixels/4;
        if (e2.getX()-e1.getX()>= len4judge) {
            Intent intent=new Intent(activity, activity.leftActivity());
            activity.startActivity(intent);
            return true;
        } else if (e1.getX() - e2.getX() >= len4judge) {
            Intent intent=new Intent(activity, activity.rightActivity());
            activity.startActivity(intent);
            return true;
        }
        return false;
    }
    
    protected boolean revolveDateView(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {
        Point p1 = new Point((int)e1.getX(), (int)e1.getY());
        Point p2 = new Point((int)e2.getX(), (int)e2.getY());
        int len4judge = AppUtils.SCREEN_METRICS.widthPixels/15;
        if(AppUtils.length(p1, p2)<=len4judge) return false;
        
        int ringIndex = AppUtils.getPointContainsRing(p1, AppConfig.DATE_COMPASS_CENTROID, activity.getRevolveRadius());
        View view = activity.findViewById(AppConfig.DATE_COMPASS_SERIES_IDS[ringIndex]);
        String tag = AppUtils.toString(view.getTag());
        int [] date_view_ids = AppConfig.DATE_VIEW_ID[Integer.parseInt(tag)];
        Log.i("revolve", "view_id_array_length==="+date_view_ids.length+", tag=="+tag);
        for(int i=0; i<date_view_ids.length; i++) {
            View dateView = activity.findViewById(date_view_ids[i]);
            String key = AppUtils.parse(AppUtils.toString(dateView.getTag()));
            Log.i("revolve", "tag:"+key+"=======>"+AppConfig.get(key));
            double degree = Math.abs(Double.parseDouble(AppConfig.get(key)));
            if(e1.getX()>=e2.getX()) {
                degree = -degree;
            }
            Log.i("revolve", "degree="+degree+",velocityX="+velocityX+", velocityY="+velocityY);
            Animation anim = AppAnimationUtils.loadRevolveAnimation(dateView, activity.center(), degree, 1000, true, controller);
            dateView.startAnimation(anim);
        }
        
        return true;
    }
}
